package ms.jacrim.tropos;

import org.w3c.dom.Element;

public abstract class Relationship extends TroposObject {

	private String sourceId, targetId;
	private TroposObject source, target;

	public Relationship(Diagram diagram, TroposObject source, TroposObject target) {
		super(diagram);
		setSource(source);
		setTarget(target);
	}

	public Relationship(Diagram diagram, Element element) {
		super(diagram, element);
	}

	void populate(Element element) {
		super.populate(element);

		this.sourceId = element.getAttribute("source");
		this.targetId = element.getAttribute("target");
	}

	public String getSourceId() {
		return sourceId;
	}

	public String getTargetId() {
		return targetId;
	}

	public TroposObject getSource() {
		return (source != null && source.getId().equalsIgnoreCase(sourceId) ? source : (source = diagram.getObjectById(sourceId)));
	}

	public void setSource(TroposObject value) {
		sourceId = (source = value) != null ? value.getId() : null;
	}

	public TroposObject getTarget() {
		return (target != null && target.getId().equalsIgnoreCase(targetId) ? target : (target = diagram.getObjectById(targetId)));
	}

	public void setTarget(TroposObject value) {
		targetId = (target = value) != null ? value.getId() : null;
	}
	
	protected abstract int NotationEdgeType();
	
	@Override
	void toString(StringBuilder result, int part, int indent) {
		if (part == 0) {
			
		}
		else {
			writeIndent(result, indent);
			result.append(String.format("<edges xmi:type=\"notation:Edge\" xmi:id=\"%s\" type=\"%d\" element=\"%s\" source=\"%s\" target=\"%s\">\r\n",
					UID.generate().toString(), NotationEdgeType(),this.getId(),  this.getSource().getNotationID().toString(), this.getTarget().getNotationID().toString()));
			
			writeIndent(result, indent + 1);
		    result.append(String.format("<styles xmi:type=\"notation:RoutingStyle\" xmi:id=\"%s\"/>\r\n", UID.generate().toString()));  
		    writeIndent(result, indent + 1);
		    result.append(String.format("<styles xmi:type=\"notation:FontStyle\" xmi:id=\"%s\" fontName=\"Segoe UI\"/>\r\n", UID.generate().toString()));
		    writeIndent(result, indent + 1);
		    result.append(String.format("<styles xmi:type=\"notation:LineStyle\" xmi:id=\"%s\"/>\r\n", UID.generate().toString()));
		    writeIndent(result, indent + 1);
		    result.append(String.format("<bendpoints xmi:type=\"notation:RelativeBendpoints\" xmi:id=\"%s\" points=\"[-50, 28, 53, -50]$[-127, 71, -24, -7]\"/>\r\n", UID.generate().toString()));
		    
		    writeIndent(result, indent);
		    result.append("</edges>\r\n") ;
		}
	}
}
